<?php
/**
 * @file Fellow_follow_model.php
 * @synopsis  获得用户关注及粉丝数据
 * @author tianyu
 * @version 1.0
 * @date 2015-09-25
 */


defined('BASEPATH') OR exit('No direct script access allowed');

class Fellow_follow_model extends CI_Model
{
    function __construct()
    {
        parent::__construct();
        $this->load->model('specialtym');
        $this->load->model('userm');
    }

    // 获取上次操作的时间戳
    public function getPreviousTS($user_id, $key)
    {
        if (empty($user_id)) {
            return 0;
        }

        $res = $this->db->select($key)->from('ml_fellow_status')->where('uid', $user_id)->get()->row();
        $result = (array)$res;
        return $result;
    }


    // 设置最近操作时间：查看好友列表，查看粉丝，查看关注
    public function updateMyStatus($user_id, $key)
    {
        $tmp_id = strval($user_id);
        if (empty($tmp_id)) {
            return false;
        }

        $data = array($key => strval(time()), "uid" => $tmp_id);
        $sql = $this->db->insert_string("ml_fellow_status", $data) .
            " ON DUPLICATE KEY UPDATE {$key}='" . time() . "'";
        $this->db->query($sql);

        return true;
    }

    public function insert_hello_log($src_uid, $dest_uid, $message)
    {
        $addtime = time();
        $this->db->insert("ml_sayhello_log", array("uid1" => $src_uid, "uid2" => $dest_uid, "message" => $message, "addtime" => $addtime));
        return $this->db->insert_id();
    }

    public function hello_list($user_id, $page, $page_size)
    {
        $start = ($page - 1) * $page_size;
        $res = $this->db->select()->from('ml_sayhello_log')->where('uid2', $user_id)->order_by("addtime", "desc")->limit($page_size, $start)->get()->result_array();
        $last_time = $this->last_hellolist_time($user_id);
        $data = array();
        foreach ($res as &$row) {
            $uid1 = $row['uid1'];
            $userinfo = $this->userm->get_one_user_4_search($uid1, $user_id);
            if ($row['addtime'] > $last_time) {
                $row['is_new_hello'] = 1;
            }
            $row['uid1_userinfo'] = $userinfo;
            $data[] = $row;
        }
        return $data;
    }

    public function last_hellolist_time($user_id)
    {
        $res = $this->db->select("last_time")->from('ml_sayhello_userlog')->where('user_id', $user_id)->get()->row();
        $res = (array)$res;
        return $res ? $res['last_time'] : 0;
    }

    public function record_lasttime($user_id)
    {
        $tmp_id = strval($user_id);
        if (empty($tmp_id)) {
            return false;
        }

        $key = 'last_time';
        $data = array($key => strval(time()), "user_id" => $tmp_id);
        $sql = $this->db->insert_string("ml_sayhello_userlog", $data) .
            " ON DUPLICATE KEY UPDATE {$key}='" . time() . "'";
        $this->db->query($sql);

        return true;
    }

    public function new_hello_count($user_id)
    {
        $last_time = $this->last_hellolist_time($user_id);
        $count = $this->db->select()->from('ml_sayhello_log')->where(array('uid2' => $user_id, 'addtime >' => $last_time))->count_all_results();
        return $count;
    }

    /**
     * 获取用户关注的用户IDs
     * @param $user_id
     * @return array
     */
    public function get_user_following_ids($user_id)
    {
        $query = $this->db->select('followers')->from('ml_member_relation')->where('following', $user_id)->get()->result_array();
        if (empty($query)) return array();

        $user_ids = array_column($query, 'followers');
        if(!$user_ids) return array();

        //去掉数组里的空值，去掉官方微博
        foreach($user_ids as $key => $value) {
            if (!$value || $value == 31495 || $value == $user_id) {
                unset($user_ids[$key]);
            }
        }

        $user_ids = array_values($user_ids);
        if (!$user_ids) return array();
        return $user_ids;
    }
}
